package xx.iverson.listening_manager.service.impl;

import org.springframework.stereotype.Service;
import xx.iverson.listening_manager.entity.CategoryLevel1;
import xx.iverson.listening_manager.entity.CategoryLevel2;
import xx.iverson.listening_manager.mapper.CategoryMapper;
import xx.iverson.listening_manager.result.ReturnResult;
import xx.iverson.listening_manager.service.CategoryService;

import javax.annotation.Resource;
import java.util.List;

/**
 * xx.iverson.listening_manager.service.impl.CategoryServiceImpl
 * 描述：
 * on 2025/10/16-下午5:43
 *
 * @author weijx
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    // 一级分类方法
    @Override
    public List<CategoryLevel1> getAllLevel1() {
        return categoryMapper.selectAllLevel1();
    }

    @Override
    public ReturnResult<CategoryLevel1> getLevel1ById(Integer categoryId) {
        try {
            CategoryLevel1 category = categoryMapper.selectLevel1ById(categoryId);
            if (category == null) {
                return ReturnResult.notFound();
            }
            return ReturnResult.success(category);
        } catch (Exception e) {
            return ReturnResult.error(500, "查询失败");
        }
    }

    @Override
    public ReturnResult<Boolean> addLevel1(CategoryLevel1 category) {
        try {
            if (category.getName() == null || category.getName().trim().isEmpty()) {
                return ReturnResult.error(400, "分类名称不能为空");
            }

            int result = categoryMapper.insertLevel1(category);
            return result > 0 ?
                    ReturnResult.success("添加成功", true) :
                    ReturnResult.error(500, "添加失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "添加失败: " + e.getMessage(), false);
        }
    }

    @Override
    public ReturnResult<Boolean> updateLevel1(CategoryLevel1 category) {
        try {
            if (category.getCategoryId() == null) {
                return ReturnResult.error(400, "分类ID不能为空");
            }
            if (category.getName() == null || category.getName().trim().isEmpty()) {
                return ReturnResult.error(400, "分类名称不能为空");
            }

            int result = categoryMapper.updateLevel1(category);
            return result > 0 ?
                    ReturnResult.success("更新成功", true) :
                    ReturnResult.error(500, "更新失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "更新失败: " + e.getMessage(), false);
        }
    }

    @Override
    public ReturnResult<Boolean> deleteLevel1(Integer categoryId) {
        try {
            // 检查是否有二级分类
            int level2Count = categoryMapper.countLevel2ByCategoryId(categoryId);
            if (level2Count > 0) {
                return ReturnResult.error(400, "该分类下存在二级分类，无法删除");
            }

            int result = categoryMapper.deleteLevel1(categoryId);
            return result > 0 ?
                    ReturnResult.success("删除成功", true) :
                    ReturnResult.error(500, "删除失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "删除失败: " + e.getMessage(), false);
        }
    }

    // 二级分类方法
    @Override
    public List<CategoryLevel2> getAllLevel2() {
        return categoryMapper.selectAllLevel2();
    }

    @Override
    public List<CategoryLevel2> getLevel2ByCategoryId(Integer categoryId) {
        return categoryMapper.selectLevel2ByCategoryId(categoryId);
    }

    @Override
    public ReturnResult<CategoryLevel2> getLevel2ById(Integer subcategoryId) {
        try {
            CategoryLevel2 subcategory = categoryMapper.selectLevel2ById(subcategoryId);
            if (subcategory == null) {
                return ReturnResult.notFound();
            }
            return ReturnResult.success(subcategory);
        } catch (Exception e) {
            return ReturnResult.error(500, "查询失败");
        }
    }

    @Override
    public ReturnResult<Boolean> addLevel2(CategoryLevel2 subcategory) {
        try {
            if (subcategory.getCategoryId() == null) {
                return ReturnResult.error(400, "一级分类ID不能为空");
            }
            if (subcategory.getSubcategoryName() == null || subcategory.getSubcategoryName().trim().isEmpty()) {
                return ReturnResult.error(400, "二级分类名称不能为空");
            }

            int result = categoryMapper.insertLevel2(subcategory);
            return result > 0 ?
                    ReturnResult.success("添加成功", true) :
                    ReturnResult.error(500, "添加失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "添加失败: " + e.getMessage(), false);
        }
    }

    @Override
    public ReturnResult<Boolean> updateLevel2(CategoryLevel2 subcategory) {
        try {
            if (subcategory.getSubcategoryId() == null) {
                return ReturnResult.error(400, "二级分类ID不能为空");
            }
            if (subcategory.getCategoryId() == null) {
                return ReturnResult.error(400, "一级分类ID不能为空");
            }
            if (subcategory.getSubcategoryName() == null || subcategory.getSubcategoryName().trim().isEmpty()) {
                return ReturnResult.error(400, "二级分类名称不能为空");
            }

            int result = categoryMapper.updateLevel2(subcategory);
            return result > 0 ?
                    ReturnResult.success("更新成功", true) :
                    ReturnResult.error(500, "更新失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "更新失败: " + e.getMessage(), false);
        }
    }

    @Override
    public ReturnResult<Boolean> deleteLevel2(Integer subcategoryId) {
        try {
            // 检查是否有文件
            int audioCount = categoryMapper.countAudioBySubcategoryId(subcategoryId);
            if (audioCount > 0) {
                return ReturnResult.error(400, "该分类下存在听力文件，无法删除");
            }

            int result = categoryMapper.deleteLevel2(subcategoryId);
            return result > 0 ?
                    ReturnResult.success("删除成功", true) :
                    ReturnResult.error(500, "删除失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "删除失败: " + e.getMessage(), false);
        }
    }
}
